package com.araaya.eslimsmobile;


import java.io.FileNotFoundException;

import com.araaya.eslimsmobile.R;
import com.araaya.eslimsmobile.util.ElandConstant;
import com.araaya.eslimsmobile.util.PropertyReader;
import com.araaya.eslimsmobile.web.TestFileUpload;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class RunWebSite extends Activity {

	WebView webView;

	private ValueCallback<Uri> mUploadMessage;
	private final static int FILECHOOSER_RESULTCODE = 1;

	public static int scalewebview;

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		if (requestCode == FILECHOOSER_RESULTCODE) {
			if (null == mUploadMessage)
				return;
			Uri result = intent == null || resultCode != RESULT_OK ? null
					: intent.getData();
			mUploadMessage.onReceiveValue(result);
			mUploadMessage = null;
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.web_home);

		// WebView myWebView = (WebView) findViewById(R.id.webview);
		// myWebView.loadUrl("http://192.168.1.102:8080/eLand");

		webView = (WebView) findViewById(R.id.webview);

		webView.clearCache(true);
		/*
		 * if (getApplicationContext().deleteDatabase("webview.db")) {
		 * Log.d("DB===>", "DELETE...."); }
		 * 
		 * if (getApplicationContext().deleteDatabase("webviewCache.db")) {
		 * Log.d("DB===>", "Cash delete...."); }
		 */

		WebSettings webSettings = webView.getSettings();
		webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
		webSettings.setJavaScriptEnabled(true);

		webSettings.setBuiltInZoomControls(true);
		// webSettings.setDefaultZoom(10);
		webSettings.setUserAgent(0);

		webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

		/*String coordinate = getIntent().getExtras().getString("coordinate")
				.trim();
		String middlePoint = getIntent().getExtras().getString("middlePoint")
				.trim();
		String refNo = "" + getIntent().getExtras().getLong("refNo");*/
		//Log.d("eSlims", "Site_URL : "+ ElandConstant.SITE_URL);
		//Toast.makeText(RunWebSite.this, "Site_URL : "+ ElandConstant.Mobile_ACTION_URL  , Toast.LENGTH_LONG).show();
		
		String site_url;
		try{
			site_url = PropertyReader.loadProperties();
			site_url +="welcomeMobile.do";
		}catch (FileNotFoundException e){
			site_url =  ElandConstant.Mobile_ACTION_URL;
		}

		webView.loadUrl(site_url);
		// webView.loadUrl("http://192.168.2.242/eLand");

		// webView.loadUrl("http://23.23.201.229/eLand");
		webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
		webView.getSettings().setLoadWithOverviewMode(true);
		webView.getSettings().setUseWideViewPort(true);
		webView.setPadding(0, 0, 0, 0);

		// webView.setInitialScale(100);
		webView.setWebViewClient(new MyWebViewClient());

		// webView.loadUrl("http://23.23.201.229/eLand");

		// webView.loadUrl("http://192.168.1.102:8080/eLand/editDetailLandInformationMobile.do?landId=17");
		// webView.setScrollContainer(true);
		// webView.setWebViewClient(new MyWebViewClient());
		// webView.setWebChromeClient(new WebChromeClient());

		// webView.loadUrl("file:///android_asset/web/index.html");
		// webView.setWebChromeClient(new WebChromeClient());

		/*
		 * webView.setWebViewClient(new WebViewClient()
		 * 
		 * {
		 * 
		 * @SuppressWarnings("unused") public void
		 * openFileChooser(ValueCallback<Uri> uploadMsg) {
		 * 
		 * mUploadMessage = uploadMsg; Intent i = new
		 * Intent(Intent.ACTION_GET_CONTENT);
		 * i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*");
		 * WebHome.this.startActivityForResult( Intent.createChooser(i,
		 * "File Chooser"), FILECHOOSER_RESULTCODE);
		 * 
		 * }
		 * 
		 * 
		 * public boolean shouldOverrideUrlLoading(WebView view, String url) {
		 * Log.d("Cache===>", "Call Webclient...."); view.loadUrl(url);
		 * 
		 * return true; }
		 * 
		 * });
		 */

	}

	private class MyWebViewClient extends WebViewClient {
		/*
		 * @Override public boolean shouldOverrideUrlLoading(WebView view,
		 * String url) { Log.d("Cache===>", "Call Webclient...."); if
		 * (Uri.parse(url).getHost().equals("http://192.168.1.100:8080/eLand"))
		 * { // This is my web site, so do not override; let my WebView load the
		 * page return false; } // Otherwise, the link is not for a page on my
		 * site, so launch another Activity that handles URLs Intent intent =
		 * new Intent(Intent.ACTION_VIEW, Uri.parse(url));
		 * startActivity(intent); return true; }
		 */

		/*
		 * @Override public void onPageStarted(WebView view, String url, Bitmap
		 * favicon) { // TODO Auto-generated method stub Log.d("WEB===>",
		 * "Page Start...."); webView.clearCache(true);
		 * getApplicationContext().deleteDatabase("webview.db");
		 * getApplicationContext().deleteDatabase("webviewCache.db");
		 * super.onPageStarted(view, url, favicon); } *
		 */

		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			Log.d("Cache===>", "Call Webclient....");
			view.loadUrl(url);

			return true;
		}

		/*
		 * public void onPageFinished(WebView view, String url) {
		 * view.setInitialScale((int) (100 * view.getScale())); }
		 */

		/*
		 * @Override public void onPageFinished(WebView view, String url) { //
		 * TODO Auto-generated method stub Log.d("WEB===>", "Page Finish....");
		 * webView.clearCache(true); webView.clearHistory(); }
		 */

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// Check if the key event was the Back button and if there's history
		/*
		 * if ((keyCode == KeyEvent.KEYCODE_MENU) && webView.canGoBack()) {
		 * Log.d("WEB===>", "Back Call...."); //webView.clearCache(true);
		 * webView.goBack(); return true; }else{ finish(); //Intent intent =
		 * (Intent) new Intent(WebHome.this, ElandMobileHome.class);
		 * //startActivity(intent); }
		 */
		if ((keyCode == KeyEvent.KEYCODE_MENU) && webView.canGoBack()) {
			Log.d("WEB===>", "Back Call....");
			// webView.clearCache(true);
			finish();
		}

		// If it wasn't the Back key or there's no web page history, bubble up
		// to the default
		// system behavior (probably exit the activity)
		return super.onKeyDown(keyCode, event);
	}
}

